package v4.text;

public class HexKit {

	public static byte[] hexToBytes(String hex) {
	    hex = hex.length() % 2 != 0 ? "0" + hex : hex;
	 
	    byte[] b = new byte[hex.length() / 2];
	    for (int i = 0; i < b.length; i++) {
	        int index = i * 2;
	        int v = Integer.parseInt(hex.substring(index, index + 2), 16);
	        b[i] = (byte) v;
	    }
	    return b;
	}

	private static char[] hexArray = "0123456789abcdef".toCharArray();
	public static String bytesToHex(byte[] bytes) {
	    char[] hexChars = new char[bytes.length * 2];
	 
	    for (int j = 0; j < bytes.length; j++) {
	        int v = bytes[j] & 0xFF;
	        hexChars[j * 2] = hexArray[v >>> 4];
	        hexChars[j * 2 + 1] = hexArray[v & 0x0F];
	    }
	    return new String(hexChars);
	}
	
}
